package com.imooc.sellspringcloud.server.controller;


import com.imooc.product.common.DecreaseStockInput;
import com.imooc.product.common.ProductInfoOutput;
import com.imooc.sellspringcloud.server.VO.ProductInfoVO;
import com.imooc.sellspringcloud.server.VO.ProductVO;
import com.imooc.sellspringcloud.server.VO.ResultVO;
import com.imooc.sellspringcloud.server.dataobject.ProductCategory;
import com.imooc.sellspringcloud.server.dataobject.ProductInfo;
import com.imooc.sellspringcloud.server.service.ProductCategoryService;
import com.imooc.sellspringcloud.server.service.ProductInfoService;
import com.imooc.sellspringcloud.server.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 * @create 2019-07-27 17:15
 **/
@RestController
public class ProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping(value = "/list", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultVO list() {
        List<ProductInfo> productInfos = productInfoService.findUpAll();

        if(productInfos == null || productInfos.isEmpty()){
            ResultVOUtil.success(null);
        }

        List<Integer> categorys = productInfos.stream()
                .map(ProductInfo::getCategoryType).collect(Collectors.toList());

        List<ProductCategory> productCategoryList = productCategoryService
                .getProductCategoryByType(categorys);

        if(productCategoryList == null || productCategoryList.isEmpty()){
            ResultVOUtil.success(null);
        }

        List<ProductVO> productVOList = new ArrayList<>();

        for (ProductCategory category : productCategoryList) {
            ProductVO vo = new ProductVO();
            vo.setCategoryName(category.getCategoryName());
            vo.setCategoryType(category.getCategoryType());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            vo.setProductInfoVOList(productInfoVOList);
            for (ProductInfo info : productInfos) {
                if(info.getCategoryType().equals(category.getCategoryType())) {
                    ProductInfoVO infoVO = new ProductInfoVO();
                    try {
                        BeanUtils.copyProperties(info, infoVO);
                        productInfoVOList.add(infoVO);
                    } catch (BeansException e) {
                        // e.printStackTrace();
                    }
                }
            }
            productVOList.add(vo);
        }

        return ResultVOUtil.success(productVOList);
    }

    @PostMapping("/getPordutsByIds")
    public List<ProductInfoOutput> getProductInfoByIds(@RequestBody List<String> ids) {
        List<ProductInfo> result = productInfoService.findProductsByIds(ids);
        List<ProductInfoOutput> productInfoOutputs = new ArrayList<>();
        for (ProductInfo productInfo : result) {
            ProductInfoOutput output = new ProductInfoOutput();
            BeanUtils.copyProperties(productInfo,output);
            productInfoOutputs.add(output);
        }
        return productInfoOutputs;
    }

    @PostMapping("/reduceProductStock")
    public ResultVO reduceProductStock(@RequestBody List<DecreaseStockInput> decreaseStockInputs) {
        boolean result = productInfoService.reduceProductByIds(decreaseStockInputs);
        return ResultVOUtil.success(result);
    }

    @GetMapping("/getAllProduct")
    public  List<ProductInfoOutput> getAllProductList(){
        List<ProductInfo> productInfos = productInfoService.findUpAll();
        if(productInfos == null || productInfos.isEmpty()){
            return null;
        }
/*        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        List<ProductInfoOutput> resultList = new ArrayList<>();
        for (ProductInfo productInfo : productInfos) {
            ProductInfoOutput output = new ProductInfoOutput();
            BeanUtils.copyProperties(productInfo,output);
            resultList.add(output);
        }
        return resultList;
    }

    @GetMapping("/msg")
    public String getMsg() {
        return "this is product project";
    }
}